<%@Page Language="VB" %>
<%@Import Namespace="System.XML" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Accessing an XML document with an XMLTextReader object</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Accessing an XML document with an XMLTextReader object</span><hr />
<!--------------------------------------------------------------------------->

<div id="outDocURL" runat="server"></div>
<div id="outError" runat="server">&nbsp;</div>
<div id="outResults" runat="server"></div>

<script language="vb" runat="server">

Sub Page_Load()

   'create physical path to booklist.xml sample file (in same folder as ASPX page)
   Dim strCurrentPath As String = Request.PhysicalPath
   Dim strXMLPath As String = Left(strCurrentPath, InStrRev(strCurrentPath, "\")) & "booklist.xml"

   'declare a variable to hold an XmlTextReader object
   Dim objXMLReader As XmlTextReader

   Try

      'create a new XmlTextReader object for the XML file
      objXMLReader = New XmlTextReader(strXMLPath)
      outDocURL.innerHTML = "Opened file: <b>" & strXMLPath & "</b>"

   Catch objError As Exception

      'display error details
      outError.innerHTML = "<b>* Error while accessing document</b>.<br />" _
          & objError.Message & "<br />" & objError.Source
      Exit Sub  ' and stop execution

   End Try

   'now ready to read (or "pull") the nodes of the XML document
   Dim strNodeResult As String = ""
   Dim objNodeType As XmlNodeType

   'read each node in turn - returns False if no more nodes to read
   Do While objXMLReader.Read()

      'select on the type of the node (these are only some of the types)
      objNodeType = objXMLReader.NodeType

      Select Case objNodeType

        Case XmlNodeType.XmlDeclaration:
           'get the name and value
           strNodeResult += "XML Declaration: <b>" & objXMLReader.Name _
                         & " " & objXMLReader.Value & "</b><br />"

        Case XmlNodeType.Element:
           'just get the name, any value will be in next (#text) node
           strNodeResult += "Element: <b>" & objXMLReader.Name & "</b><br />"

        Case XmlNodeType.Text:
           'just display the value, node name is "#text" in this case
           strNodeResult += "&nbsp; - Value: <b>" & objXMLReader.Value _
                         & "</b><br />"

      End Select

      'see if this node has any attributes
      If objXMLReader.AttributeCount > 0 Then

         'iterate through the attributes by moving to the next one
         'could use MoveToFirstAttribute but MoveToNextAttribute does
         'the same when the current node is an element-type node
         Do While objXMLReader.MoveToNextAttribute()

            'get the attribute name and value
            strNodeResult +=  "&nbsp; - Attribute: <b>" & objXMLReader.Name _
                          & "</b> &nbsp; Value: <b>" & objXMLReader.Value _
                          & "</b><br />"
         Loop

      End If

   Loop   'and read the next node

   'finished with the reader so close it
   objXMLReader.Close()

   'and display the results in the page
   outResults.innerHTML = strNodeResult

End Sub

</script>

<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
